<?php
/**
 * 服务管理
 *
 */
namespace Admin\Controller;
use Think\Controller;
use Common\Controller\BaseController;

class QyServiceController extends BaseController {

	/*列表*/
	public function index(){
		$res = array('rcode'=>1,'data'=>NULL, 'msg'=> 'OK');
		$psize = I('get.psize', $this->m_psize,'intval');
		$name = I('get.name','','trim');
		$type = I('get.type',false,'intval');
		if( !$psize )
			$psize = $this->m_psize;
		else if( $psize > 20 )
			$psize = $this->m_psize;

		$MQyService = D("qy_service");
		$MQY = M("qy");
		$wheres = array();
		$wheres['name'] = array('like', '%'.$name.'%');
		$wheres['is_deleted'] = array('neq', 1);
		if($type==-1)
			$wheres['type'] = array('in', [0,1,2]);
		else if($type!=-1)
			$wheres['type'] = array('eq', $type);
		$cnt = $MQyService->where( $wheres )->count();
		$Page = new \Common\ORG\Page( $cnt, $psize );
		$service_list = $MQyService->field("*")
						->where($wheres)
						->order("service_id desc")
			            ->limit($Page->firstRow.','.$Page->listRows )
			            ->select();
		$sql = 	$MQyService->getLastSql();     	
		if( $service_list ){
  			$res['rcode'] = 1;
  			$res['msg'] = "OK";
  			$res['data'] = $service_list;
  			$res['pall'] =  $Page->getTotalPages();
  			$res['p'] = $Page->nowPage;
		}else{
	       	$res['rcode'] = 2;
			$res['msg'] = "产品线信息列表为空";
		}
		$res['sql'] = $sql;
		e_json( $res );
	}


	public function addService(){
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		$add_data = array();
		$add_data['name'] = I('post.add_name','','trim');
		$add_data['type'] = I('post.add_type',false,'intval');
		$add_data['remark'] = I('post.add_remark','','trim');	//备注
		$addres = D('QyService')->add_service($add_data);
		$res['rcode'] = $addres['r'];
		$res['data'] = $addres['d'];
		$res['msg'] = $addres['i'];
		e_json( $res );
	}

	/*编辑服务*/
	public function editService(){
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		$edit_data = array();
		$edit_data['service_id'] = I('post.update_service_id',false,'intval');
		$edit_data['name'] = I('post.edit_name','','trim');
		$edit_data['type'] = I('post.edit_type',false,'intval');
		$edit_data['remark'] = I('post.edit_remark','','trim');	//备注
		$editres = D('QyService')->edit_service($edit_data);
		$res['rcode'] = $editres['r'];
		$res['data'] = $editres['d'];
		$res['msg'] = $editres['i'];
		e_json( $res );
	}

	/*删除服务*/
	public function delService(){
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		$service_id = I('get.service_id');
		if( !$service_id ){
			$res['msg'] = '参数错误';
		}else{
			$DQyService = D('QyService');
			$delres = $DQyService->delete_service( $service_id);
			$res['rcode'] = $delres['r'];
			$res['msg'] = $delres['i'];
		}
		e_json( $res );
	}

}
